﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.Buttons;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar
{
    public class ucHeader : Control
    {
        #region Dependency Variables
        public static readonly DependencyProperty CurrentDateProperty = DependencyProperty.Register("CurrentDate", typeof(DateTime), typeof(ucHeader), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(CurrentDate_Changed)));
        public static readonly DependencyProperty CurrentDateViewProperty = DependencyProperty.Register("CurrentDateView", typeof(Enumerations.DateView), typeof(ucHeader), new PropertyMetadata(Enumerations.DateView.Day, new PropertyChangedCallback(CurrentDateView_Changed)));
        private static readonly DependencyProperty DateInViewProperty = DependencyProperty.Register("DateInView", typeof(string), typeof(ucHeader), new PropertyMetadata(DateTime.Now.ToShortDateString()));
        private static readonly DependencyProperty WorkWeekDetailsProperty = DependencyProperty.Register("WorkWeekDetails", typeof(Visibility), typeof(ucHeader), new PropertyMetadata(Visibility.Collapsed));        
        #endregion

        #region Dependency Properties
        public DateTime CurrentDate
        {
            get { return (DateTime)this.GetValue(CurrentDateProperty); }
            set { this.SetValue(CurrentDateProperty, value); }
        }
        public string DateInView
        {
            get { return this.GetValue(DateInViewProperty).ToString(); }
            set { this.SetValue(DateInViewProperty, value); }
        }
        public Visibility WorkWeekDetails
        {
            get { return (Visibility)this.GetValue(WorkWeekDetailsProperty); }
            set { this.SetValue(WorkWeekDetailsProperty, value); }
        }
        public Enumerations.DateView CurrentDateView
        {
            get { return (Enumerations.DateView)this.GetValue(CurrentDateViewProperty); }
            set { this.SetValue(CurrentDateViewProperty, value); }
        }
        #endregion

        #region Dependency Events
        #region CurrentDate_Changed
        private static void CurrentDate_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucHeader).OnCurrentDate_Changed(e);
        }
        #endregion

        #region CurrentDateView_Changed
        private static void CurrentDateView_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucHeader).OnCurrentDateView_Changed(e);
        }
        #endregion
        #endregion

        #region Variables
        private Button btnPrevious = null;
        private Button btnNext = null;
        private ucGuideButton btnDay = null;
        private ucGuideButton btnWeek = null;
        private ucGuideButton btnMonth = null;
        private ucGuideButton btnYear = null;
        private RadioButton rbShowWorkWeek = null;
        private RadioButton rbShowFullWeek = null;
        #endregion

        #region Event Variables
        public event EventHandler Day_Clicked;
        public event EventHandler WorkWeek_Clicked;
        public event EventHandler FullWeek_Clicked;
        public event EventHandler Month_Clicked;
        public event EventHandler Year_Clicked;
        public event EventHandler Date_Changed;
        #endregion

        #region Constructor
        public ucHeader()
        {
            DefaultStyleKey = typeof(ucHeader);
        }
        #endregion

        #region Methods
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            btnPrevious = (Button)base.GetTemplateChild("btnPrevious");
            btnNext = (Button)base.GetTemplateChild("btnNext");
            btnDay = (ucGuideButton)base.GetTemplateChild("btnDay");
            btnWeek = (ucGuideButton)base.GetTemplateChild("btnWeek");
            btnMonth = (ucGuideButton)base.GetTemplateChild("btnMonth");
            btnYear = (ucGuideButton)base.GetTemplateChild("btnYear");
            rbShowWorkWeek = (RadioButton)base.GetTemplateChild("rbShowWorkWeek");
            rbShowFullWeek = (RadioButton)base.GetTemplateChild("rbShowFullWeek");

            if (btnDay == null) return;
            if (btnWeek == null) return;
            if (btnMonth == null) return;
            if (btnYear == null) return;

            btnDay.Click += new RoutedEventHandler(btnDay_Click);
            btnWeek.Click += new RoutedEventHandler(btnWeek_Click);
            btnMonth.Click += new RoutedEventHandler(btnMonth_Click);
            btnYear.Click += new RoutedEventHandler(btnYear_Click);

            if (rbShowWorkWeek == null) return;
            if (rbShowFullWeek == null) return;

            rbShowWorkWeek.Checked += new RoutedEventHandler(rbShowWorkWeek_Checked);
            rbShowFullWeek.Checked += new RoutedEventHandler(rbShowFullWeek_Checked);

            if (btnPrevious == null) return;
            if (btnNext == null) return;
            
            btnPrevious.Click += new RoutedEventHandler(btnPrevious_Click);
            btnNext.Click += new RoutedEventHandler(btnNext_Click);

            CurrentDateView = Enumerations.DateView.Day;
            SetDateInView(CurrentDateView);
        }
        #endregion

        #region SetDateInView
        private void SetDateInView(Enumerations.DateView _CurrentDateView)
        {
            CurrentDateView = _CurrentDateView;

            switch (_CurrentDateView)
            {
                case Enumerations.DateView.Day:
                    if (btnDay != null)
                    {
                        btnDay.ButtonIsPressed = true;
                        btnWeek.ButtonIsPressed = false;
                        btnMonth.ButtonIsPressed = false;
                        btnYear.ButtonIsPressed = false;
                        WorkWeekDetails = Visibility.Collapsed;
                    }

                    DateInView = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonthDayNumberYear, CurrentDate);
                    break;
                case Enumerations.DateView.WorkWeek:
                    if (btnWeek != null)
                    {
                        btnDay.ButtonIsPressed = false;
                        btnWeek.ButtonIsPressed = true;
                        btnMonth.ButtonIsPressed = false;
                        btnYear.ButtonIsPressed = false;
                        WorkWeekDetails = Visibility.Visible;
                    }

                    if (CurrentDate.DayOfWeek != System.DayOfWeek.Monday && CurrentDate.DayOfWeek != System.DayOfWeek.Sunday)
                        CurrentDate = CurrentDate.AddDays((int)System.DayOfWeek.Monday - (int)CurrentDate.DayOfWeek);

                    DateInView = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonthDayNumberYear, CurrentDate) + " - " + SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonthDayNumberYear, CurrentDate.AddDays(4));
                    break;
                case Enumerations.DateView.FullWeek:
                    if (btnWeek != null)
                    {
                        btnDay.ButtonIsPressed = false;
                        btnWeek.ButtonIsPressed = true;
                        btnMonth.ButtonIsPressed = false;
                        btnYear.ButtonIsPressed = false;
                        WorkWeekDetails = Visibility.Visible;
                    }

                    if (CurrentDate.DayOfWeek != System.DayOfWeek.Monday && CurrentDate.DayOfWeek != System.DayOfWeek.Sunday)
                        CurrentDate = CurrentDate.AddDays((int)System.DayOfWeek.Monday - (int)CurrentDate.DayOfWeek);
                    else if (CurrentDate.DayOfWeek == System.DayOfWeek.Sunday)
                        CurrentDate = CurrentDate.AddDays(-6);

                    DateInView = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonthDayNumberYear, CurrentDate) + " - " + SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonthDayNumberYear, CurrentDate.AddDays(6));                    
                    break;
                case Enumerations.DateView.Month:
                    if (btnMonth != null)
                    {
                        btnDay.ButtonIsPressed = false;
                        btnWeek.ButtonIsPressed = false;
                        btnMonth.ButtonIsPressed = true;
                        btnYear.ButtonIsPressed = false;
                        WorkWeekDetails = Visibility.Collapsed;
                    }

                    DateInView = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonthYear, CurrentDate);
                    break;
                case Enumerations.DateView.Year:
                    if (btnMonth != null)
                    {
                        btnDay.ButtonIsPressed = false;
                        btnWeek.ButtonIsPressed = false;
                        btnMonth.ButtonIsPressed = false;
                        btnYear.ButtonIsPressed = true;
                        WorkWeekDetails = Visibility.Collapsed;
                    }

                    DateInView = CurrentDate.Year.ToString();
                    break;
            }
        }
        #endregion

        #region OnCurrentDate_Changed
        protected virtual void OnCurrentDate_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDate = (DateTime)e.NewValue;
            SetDateInView(CurrentDateView);
        }
        #endregion

        #region OnCurrentDateView_Changed
        protected virtual void OnCurrentDateView_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDateView = (Enumerations.DateView)e.NewValue;
            SetDateInView(CurrentDateView);
        }
        #endregion
        #endregion

        #region Events
        #region btnNext_Click
        void btnNext_Click(object sender, RoutedEventArgs e)
        {
            switch (CurrentDateView)
            {
                case Enumerations.DateView.Day:
                    CurrentDate = CurrentDate.AddDays(1);
                    break;
                case Enumerations.DateView.WorkWeek:
                case Enumerations.DateView.FullWeek:
                    CurrentDate = CurrentDate.AddDays(7);
                    break;
                case Enumerations.DateView.Month:
                    CurrentDate = CurrentDate.AddMonths(1);
                    break;
                case Enumerations.DateView.Year:
                    CurrentDate = CurrentDate.AddYears(1);
                    break;
            }

            if (Date_Changed != null)
                Date_Changed(CurrentDate, null);
        }
        #endregion

        #region btnPrevious_Click
        void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            switch (CurrentDateView)
            {
                case Enumerations.DateView.Day:
                    CurrentDate = CurrentDate.AddDays(-1);
                    break;
                case Enumerations.DateView.WorkWeek:
                case Enumerations.DateView.FullWeek:
                    CurrentDate = CurrentDate.AddDays(-7);
                    break;
                case Enumerations.DateView.Month:
                    CurrentDate = CurrentDate.AddMonths(-1);
                    break;
                case Enumerations.DateView.Year:
                    CurrentDate = CurrentDate.AddYears(-1);
                    break;
            }

            if (Date_Changed != null)
                Date_Changed(CurrentDate, null);
        }
        #endregion

        #region btnDay_Click
        private void btnDay_Click(object sender, RoutedEventArgs e)
        {
            if (Day_Clicked != null)
                Day_Clicked(this, null);

            WorkWeekDetails = Visibility.Collapsed;

            btnDay.ButtonIsPressed = true;
            btnWeek.ButtonIsPressed = false;
            btnMonth.ButtonIsPressed = false;
            btnYear.ButtonIsPressed = false;

            SetDateInView(Enumerations.DateView.Day);
        }
        #endregion

        #region btnWeek_Click
        private void btnWeek_Click(object sender, RoutedEventArgs e)
        {
            if (WorkWeek_Clicked != null)
                WorkWeek_Clicked(this, null);

            btnDay.ButtonIsPressed = false;
            btnWeek.ButtonIsPressed = true;
            btnMonth.ButtonIsPressed = false;
            btnYear.ButtonIsPressed = false;

            SetDateInView(Enumerations.DateView.WorkWeek);
        }
        #endregion

        #region btnMonth_Click
        private void btnMonth_Click(object sender, RoutedEventArgs e)
        {
            if (Month_Clicked != null)
                Month_Clicked(this, null);

            WorkWeekDetails = Visibility.Collapsed;

            btnDay.ButtonIsPressed = false;
            btnWeek.ButtonIsPressed = false;
            btnMonth.ButtonIsPressed = true;
            btnYear.ButtonIsPressed = false;

            SetDateInView(Enumerations.DateView.Month);
        }
        #endregion

        #region btnYear_Click
        private void btnYear_Click(object sender, RoutedEventArgs e)
        {
            if (Year_Clicked != null)
                Year_Clicked(this, null);

            WorkWeekDetails = Visibility.Collapsed;

            btnDay.ButtonIsPressed = false;
            btnWeek.ButtonIsPressed = false;
            btnMonth.ButtonIsPressed = false;
            btnYear.ButtonIsPressed = true;

            SetDateInView(Enumerations.DateView.Year);
        }
        #endregion

        #region rbShowWorkWeek_Checked
        void rbShowWorkWeek_Checked(object sender, RoutedEventArgs e)
        {
            if (((RadioButton)sender).IsChecked.Value)
            {
                if (WorkWeek_Clicked != null)
                    WorkWeek_Clicked(this, null);

                SetDateInView(Enumerations.DateView.WorkWeek);
            }
        }
        #endregion

        #region rbShowFullWeek_Checked
        private void rbShowFullWeek_Checked(object sender, RoutedEventArgs e)
        {
            if (((RadioButton)sender).IsChecked.Value)
            {
                if (FullWeek_Clicked != null)
                    FullWeek_Clicked(this, null);

                SetDateInView(Enumerations.DateView.FullWeek);
            }
        }
        #endregion
        #endregion       
    }
}
